package ljye.com.ljye.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.loader.autohideime.HideIMEUtil;

import ljye.com.ljye.R;
import ljye.com.ljye.utils.Utils;

//搜索页面
public class Activity_Search extends Activity implements View.OnClickListener {
    private ImageView ivDeleteText;
    private EditText etSearch;
    private ImageView back;
    private TextView tvSearch;//搜索/取消按钮
    private boolean isSearch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__search);
        HideIMEUtil.wrap(this);//点击其他位置收回软键盘
        Utils.setColor(Activity_Search.this, Color.parseColor("#FF8D59"));//橙色状态栏
        initView();//初始化控件
        ivDeleteText.setOnClickListener(this);
        back.setOnClickListener(this);
        etSearch.addTextChangedListener(watcher);
        tvSearch.setOnClickListener(this);
    }

    private void initView() {
        ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText);
        etSearch = (EditText) findViewById(R.id.etSearch);
        tvSearch = (TextView) findViewById(R.id.tv_search);
        back = (ImageView) findViewById(R.id.back);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ivDeleteText:
                etSearch.setText("");
                break;
            case R.id.tv_search://搜索/取消按钮
                if (isSearch){
                    //搜索


                }else{
                    //取消
                    finish();
                }
                break;
            default:
                break;
        }

    }
    private TextWatcher watcher=new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.length() == 0) {
                isSearch=false;
                tvSearch.setText("取消");
                ivDeleteText.setVisibility(View.GONE);
            } else {
                isSearch=true;
                tvSearch.setText("搜索");
                ivDeleteText.setVisibility(View.VISIBLE);
            }
        }
    };
}
